package grape.composite.rest.businessdistrict.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.common.exception.runtime.RBaseException;
import grape.composite.rest.businessdistrict.form.BusinessDistrictListForm;
import grape.composite.service.businessdistrict.query.BusinessDistrictListQuery;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.composite.rest.businessdistrict.form.BusinessDistrictCreateForm;
import grape.composite.rest.businessdistrict.form.BusinessDistrictUpdateForm;
import grape.composite.rest.businessdistrict.form.BusinessDistrictListPageForm;
import grape.composite.rest.businessdistrict.vo.BusinessDistrictVo;
import grape.composite.rest.businessdistrict.mapper.BusinessDistrictWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.composite.service.businessdistrict.po.BusinessDistrict;
import grape.composite.service.businessdistrict.api.IBusinessDistrictService;
import java.util.List;
/**
 * <p>
 * 商圈 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-09-10
 */
@RestController
@RequestMapping("/composite/businessdistrict")
@Api(tags = "商圈相关接口")
public class BusinessDistrictController extends BaseController<BusinessDistrictVo, BusinessDistrict> {

    @Autowired
    private BusinessDistrictWebMapper currentWebMapper;
    @Autowired
    private IBusinessDistrictService currentService;



     @ApiOperation("添加商圈")
     @PreAuthorize("hasAuthority('businessdistrict:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public BusinessDistrictVo create(@RequestBody @Valid BusinessDistrictCreateForm cf) {
         // code 唯一检查
         if (currentService.existCode(cf.getCode())) {
             throw new RBaseException("编码已存在");
         }
         BusinessDistrict po = currentWebMapper.formToPo(cf);
         return super.create(po);
     }

     @ApiOperation("根据ID查询商圈")
     @PreAuthorize("hasAuthority('businessdistrict:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public BusinessDistrictVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除商圈")
     @PreAuthorize("hasAuthority('businessdistrict:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新商圈")
     @PreAuthorize("hasAuthority('businessdistrict:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public BusinessDistrictVo update(@PathVariable String id,@RequestBody @Valid BusinessDistrictUpdateForm uf) {
         BusinessDistrict po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }

    @ApiOperation("不分页查询商圈")
    @PreAuthorize("hasAuthority('businessdistrict:single:list')")
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    public List<BusinessDistrictVo> list(BusinessDistrictListForm listForm) {
        BusinessDistrict po = currentWebMapper.formToPo(listForm);
        BusinessDistrictListQuery businessDistrictListQuery = new BusinessDistrictListQuery();
        businessDistrictListQuery.setPoQuery(po);
        return super.list(businessDistrictListQuery);
    }

    @ApiOperation("分页查询商圈")
    @PreAuthorize("hasAuthority('businessdistrict:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<BusinessDistrictVo> listPage(BusinessDistrictListPageForm listPageForm) {
         BusinessDistrict po = currentWebMapper.formToPo(listPageForm);
        BusinessDistrictListQuery businessDistrictListQuery = new BusinessDistrictListQuery();
        businessDistrictListQuery.setPoQuery(po);
         return super.listPage(businessDistrictListQuery,listPageForm);
     }

}
